﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace ETOrePriceCalc
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class TestWindow : Window
    {

        private List<Mineral> _DataList;
        public TestWindow(List<Mineral> list)
        {
            InitializeComponent();
            _DataList = list;            
        }

        private void parseData(string text,bool clear = false)
        {
            string[] lines = text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

            if (clear)
            {
                //clear the current stock of minerals
                foreach (Mineral m in _DataList)
                {
                    m.Stock = 0;
                }
            }

            string[] parts;
            Mineral tmpmineral = null;
            foreach (string s in lines)
            {
                parts = s.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);

                tmpmineral = getMineralByName(parts[0]);
                if(tmpmineral != null)
                    tmpmineral.Stock += XmlConvert.ToInt32(parts[1].Replace(".",""));
            }

        }

        /// <summary>
        /// returns the mineral with name name
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private Mineral getMineralByName(string name)
        {
            foreach (Mineral m in _DataList)
            {
                if (m.Name == name)
                    return m;
            }
            return null;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            parseData(textBlock1.Text);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            parseData(textBlock1.Text,true);
        }
    }
}
